package com.coolgame.login.controller;

import java.util.HashMap;
import java.util.Map;

import com.coolgame.action.base.BaseAction;
import com.coolgame.action.base.Config;
import com.coolgame.action.base.JsonUtil;
import com.coolgame.action.base.StaticSession;
import com.coolgame.login.bo.Account;
import com.coolgame.login.service.AccountServiceInter;

/**
 * 
 * @copyright : (C) Copyright cool game Corporation 2011-2020
 * @date:2013-6-24
 * @author:Caiyw
 * @since JDK1.6
 * @description:TODO
 */
public class LoginController extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Map with accounts that are active on LoginServer or joined GameServer and are not authenticated yet.
	 */
	private static final Map<Long, String> accountsOnLS = new HashMap<Long, String>();
	private AccountServiceInter accountService;
	private String retcode;
	private String account;
	private String password;
	private String clientid;

	/**
	 * Tries to authentificate account.<br>
	 * If success returns {@link AionAuthResponse#AUTHED} and sets account object to connection.<br>
	 * <p/>
	 * If {@link loginserver.configs.Config#ACCOUNT_AUTO_CREATION} is enabled - creates new account.<br>
	 * 
	 * @param name
	 *            name of account
	 * @param password
	 *            password of account
	 * @param connection
	 *            connection for account
	 * @return
	 * @return Response with error code
	 */
	public String login() {
		Account ac = accountService.loadAccount(account);

		// If account not found and not created
		if (ac == null) {
			retcode = JsonUtil.Object2Json(StaticSession.LOGIN_NOTEXIT);
			return SUCCESS;
		}

		// check for paswords beeing equals
		if (!ac.getPassword().equals(password)) {
			retcode = JsonUtil.Object2Json(StaticSession.LOGIN_PASSEORDERROR);
			return SUCCESS;
		}

		// if account is banned
		if (ac.getBanned() == 1) {
			retcode = JsonUtil.Object2Json(StaticSession.LOGIN_BANNED);
			return SUCCESS;
		}
		
		// if account is banned
		if (accountsOnLS.size() ==2000) {
			retcode = JsonUtil.Object2Json(StaticSession.LOGIN_MAXPLAYER);
			return SUCCESS;
		}

		// if account is restricted to some ip or mask
		// if (ac.getIpForce() != null) {
		// if (!NetworkUtils.checkIPMatching(account.getIpForce(), connection.getIP())) {
		// return AionAuthResponse.BAN_IP;
		// }
		// }

		// Do not allow to login two times with same account
		synchronized (LoginController.class) {

			// If someone is at loginserver, he should be disconnected
			if (accountsOnLS.containsKey(ac.getId())) {
				retcode = JsonUtil.Object2Json(StaticSession.LOGIN_LOGINED);
				return SUCCESS;
			} else {
				accountsOnLS.put(ac.getId(), String.valueOf(clientid));
				retcode = JsonUtil.Object2Json(StaticSession.LOGIN_SUCCESS);
				accountService.updateOnLogin(account, request.getRemoteAddr());
				return SUCCESS;
			}
		}

	}

	/**
	 * Kicks account from LoginServer and GameServers
	 * 
	 * @param accountId
	 *            account ID to kick
	 */
	public void kickAccount(String account) {
		synchronized (LoginController.class) {
			if (accountsOnLS.containsKey(account)) {
				accountsOnLS.remove(account);
			} else {
				// player is not online;
			}
		}
	}

	/**
	 * 登出.
	 * 
	 * @param client
	 *            对象
	 * @return true 删除集合内的账号
	 */
	public synchronized boolean logout(String account) {
		if (!accountsOnLS.containsKey(account)) {
			return false;
		}
		return this.accountsOnLS.remove(account) != null;
	}

	public String getRetcode() {
		return retcode;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setClientid(String clientid) {
		this.clientid = clientid;
	}

}
